﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonRepositoryInterface.Memory
{
    public class MemoryRepository<T> : IRepository<T>
        where T : IDataRecord
    {
        public IList<T> Items { get; private set; }

        public MemoryRepository(IRepositoryProvider provider)
            : this(provider, null)
        { }
        public MemoryRepository(IRepositoryProvider provider, IList<T> list)
        {
            this.Items = list ?? new List<T>();
            this.Provider = provider;
        }
        #region IRepository<T> Members

        public IRepositoryProvider Provider { get; private set; }
        public IQueryable<T> Query
        {
            get { return this.Items.AsQueryable(); }
        }

        public void Insert(T item)
        {
            item.DateCreated = DateTime.UtcNow;
            item.DateModified = item.DateCreated;
            this.Items.Add(item);
        }

        public void Delete(T item)
        {
            this.Items.Remove(item);
        }

        public void Update(T item)
        {
            item.DateModified = DateTime.UtcNow;
        }

        #endregion
    }
}
